package com.wtwd.device.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.LastLocationDataMapper;
import com.wtwd.device.model.entity.LastLocationData;
import com.wtwd.device.service.AppDeviceService;
import com.wtwd.device.service.ILastLocationDataService;
import com.wtwd.device.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-21
 */
@Service
public class LastLocationDataServiceImpl extends ServiceImpl<LastLocationDataMapper, LastLocationData> implements ILastLocationDataService {

    @Resource
    private AppDeviceService deviceService;

    @Resource
    private LastLocationDataMapper lastLocationDataMapper;

    @Override
    public LastLocationData getLastLocationDataByImei(String imei) {
        if (StringUtils.isEmpty(imei)) {
            return null;
        }
        Long deviceId = deviceService.getDeviceIdByImei(imei);
        if (deviceId == null) {
            return null;
        }
        return getLastLocationDataByDeviceId(deviceId);
    }

    @Override
    public LastLocationData getLastLocationDataByDeviceId(Long deviceId) {
        return new LambdaQueryChainWrapper<>(lastLocationDataMapper)
                .eq(LastLocationData::getDeviceId, deviceId)
                .one();
    }
}
